﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LittleBirdAuction.Core
{
    public static class EnumTools
    {

        public static bool FindObject(this IdEnum[] idEnums, out IdEnum findItem, string pattern, bool invert)
        {
            int   best_score = 0;
            int   best_index = AuctionConst.INVALID_INDEX;
            findItem = null;

            // search items for object with matching pattern
            for( int i = 0; i < idEnums.Length; i++ )
            {
                IdEnum item = idEnums[i];

                // substring search
                if(invert ? !string.IsNullOrEmpty(Tools.StrSearch(item.ID, pattern)) : !string.IsNullOrEmpty(Tools.StrSearch(pattern, item.ID)))
                {
                    int   score = invert ? (pattern.Length - item.ID.Length) : (item.ID.Length - pattern.Length);

                    // elect best match
                    if(score < best_score)
                    {
                        best_score = score;
                        best_index = i;
                    }
                }
            }

            if(best_index == AuctionConst.INVALID_INDEX)
                return false;

            findItem = idEnums[best_index];

            return true;
        }

        public static bool FindObject(this IdEnum[] idEnums, out IdEnum findItem, string pattern)
        {
            return FindObject(idEnums, out findItem, pattern);
        }

        public static bool FindName(this IdEnum[] idEnums, ref string name, uint itemValue)
        {

            for (int i = 0; i < idEnums.Length; i++)
            {
                IdEnum item = idEnums[i];

                if (itemValue == item.Value)
                {
                    name = item.ID;
                    return true;
                }
            }

            return false;
        }
    }
}
